<?php

/**
 * 订单(测试)
 *
 * @author cdlei
 */

require_once 'CommonController.php';

class Admin_OrderTestController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_OrderTest();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $time = time();
        $cookieModel = new Application_Model_Cookie();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        if ($this->_request->isPost()) {
            $vars['addressIsDisp'] = $this->_request->get('addressIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('addressIsDisp_order', $vars['addressIsDisp'], $time + 3600 * 24 * 30);
            $vars['appVersionIsDisp'] = $this->_request->get('appVersionIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('appVersionIsDisp_order', $vars['appVersionIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipIsDisp'] = $this->_request->get('ipIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipIsDisp_order', $vars['ipIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipProvinceIsDisp'] = $this->_request->get('ipProvinceIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipProvinceIsDisp_order', $vars['ipProvinceIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipCityIsDisp'] = $this->_request->get('ipCityIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipCityIsDisp_order', $vars['ipCityIsDisp'], $time + 3600 * 24 * 30);
            $vars['sessionIdIsDisp'] = $this->_request->get('sessionIdIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('sessionIdIsDisp_order', $vars['sessionIdIsDisp'], $time + 3600 * 24 * 30);
            $vars['udidIsDisp'] = $this->_request->get('udidIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('udidIsDisp_order', $vars['udidIsDisp'], $time + 3600 * 24 * 30);
            $vars['verIsDisp'] = $this->_request->get('verIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('verIsDisp_order', $vars['verIsDisp'], $time + 3600 * 24 * 30);
            $vars['userAgentIsDisp'] = $this->_request->get('userAgentIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('userAgentIsDisp_order', $vars['userAgentIsDisp'], $time + 3600 * 24 * 30);
        } else {
            $vars['addressIsDisp'] = $cookieModel->getCookie('addressIsDisp_order') ? 1 : 0;
            $vars['appVersionIsDisp'] = $cookieModel->getCookie('appVersionIsDisp_order') ? 1 : 0;
            $vars['ipProvinceIsDisp'] = $cookieModel->getCookie('ipProvinceIsDisp_order') ? 1 : 0;
            $vars['ipCityIsDisp'] = $cookieModel->getCookie('ipCityIsDisp_order') ? 1 : 0;
            $vars['ipIsDisp'] = $cookieModel->getCookie('ipIsDisp_order') ? 1 : 0;
            $vars['sessionIdIsDisp'] = $cookieModel->getCookie('sessionIdIsDisp_order') ? 1 : 0;
            $vars['udidIsDisp'] = $cookieModel->getCookie('udidIsDisp_order') ? 1 : 0;
            $vars['verIsDisp'] = $cookieModel->getCookie('verIsDisp_order') ? 1 : 0;
            $vars['userAgentIsDisp'] = $cookieModel->getCookie('userAgentIsDisp_order') ? 1 : 0;
        }

        $vars['orderId'] = trim($this->_request->get('orderId'));
        $vars['userName'] = trim($this->_request->get('userName'));
        $vars['fullName'] = trim($this->_request->get('fullName'));
        $vars['cellphone'] = trim($this->_request->get('cellphone'));
        $vars['province'] = trim($this->_request->get('province'));
        $vars['city'] = trim($this->_request->get('city'));
        $vars['county'] = trim($this->_request->get('county'));
        $vars['address'] = trim($this->_request->get('address'));
        $vars['udid'] = trim($this->_request->get('udid'));
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        $vars['sourceSubId'] = trim($this->_request->get('sourceSubId'));
        $vars['ip'] = trim($this->_request->get('ip'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $vars['isTest'] = trim($this->_request->get('isTest'));
        $vars['status'] = trim($this->_request->get('status'));
        $urls = $vars;

        $wheres[] = "`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."'";
        if ($vars['orderId'] != '') {
            $wheres[] = "`orderId` = '" . addslashes($vars['orderId']) . "'";
        }
        if ($vars['userName'] != '') {
            $wheres[] = "`userName` = '" . addslashes($vars['userName']) . "'";
        }
        if ($vars['fullName'] != '') {
            $wheres[] = "`fullName` = '" . addslashes($vars['fullName']) . "'";
        }
        if ($vars['cellphone'] != '') {
            $wheres[] = "`cellphone` = '" . addslashes($vars['cellphone']) . "'";
        }
        if ($vars['province'] != '') {
            $wheres[] = "`province` LIKE '%" . addslashes($vars['province']) . "%'";
        }
        if ($vars['city'] != '') {
            $wheres[] = "`city` LIKE '%" . addslashes($vars['city']) . "%'";
        }
        if ($vars['county'] != '') {
            $wheres[] = "`county` LIKE '%" . addslashes($vars['county']) . "%'";
        }
        if ($vars['address'] != '') {
            $wheres[] = "`address` LIKE '%" . addslashes($vars['address']) . "%'";
        }
        if ($vars['udid'] != '') {
            $wheres[] = "`udid` = '" . addslashes($vars['udid']) . "'";
        }
        if ($vars['osName'] != '') {
            $wheres[] = "`osName` = '" . addslashes($vars['osName']) . "'";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '" . addslashes($vars['sourceId']) . "'";
        }
        if ($vars['sourceSubId'] != '') {
            $wheres[] = "`sourceSubId` = '" . addslashes($vars['sourceSubId']) . "'";
        }
        if ($vars['ip'] != '') {
            $wheres[] = "`ip` = '" . addslashes($vars['ip']) . "'";
        }
        if ($vars['startTime'] != '') {
            $wheres[] = "`addTime` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`addTime` <= '{$vars['endTime']}' ";
        }
        if ($vars['isTest'] === '1' || $vars['isTest'] === '0') {
            $wheres[] = "`isTest` = '{$vars['isTest']}'";
        }
        if ($vars['status'] === '1' || $vars['status'] === '2') {
            $wheres[] = "`status` = '{$vars['status']}'";
        }

        $orderBy = "`addTime` DESC, `id` DESC";

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_model->getTableName()}` {$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();
        
        foreach($rows as $key=>$row) {
            $row['itemXmlParseIsError'] = 0;
            $row['itemRows'] = array();
            try{
                $xml = @new SimpleXMLElement($row['itemData']);
                foreach($xml->item as $item) {
                    $itemRow = array();
                    foreach($item->attributes() as $key2=>$value) {
                        $itemRow[$key2] = (string) $value;
                    }
                    $row['itemRows'][] = $itemRow;
                }
            } catch(Exception $e) {
                $row['itemXmlParseIsError'] = 1;
            }
            $rows[$key] = $row;
        }

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }

    /**
     * 导出动作
     * author:genjie.wang
     * @return void
     */
    public function outputAction(){
       $time = time();
        $cookieModel = new Application_Model_Cookie();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        if ($this->_request->isPost()) {
            $vars['addressIsDisp'] = $this->_request->get('addressIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('addressIsDisp_order', $vars['addressIsDisp'], $time + 3600 * 24 * 30);
            $vars['appVersionIsDisp'] = $this->_request->get('appVersionIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('appVersionIsDisp_order', $vars['appVersionIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipIsDisp'] = $this->_request->get('ipIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipIsDisp_order', $vars['ipIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipProvinceIsDisp'] = $this->_request->get('ipProvinceIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipProvinceIsDisp_order', $vars['ipProvinceIsDisp'], $time + 3600 * 24 * 30);
            $vars['ipCityIsDisp'] = $this->_request->get('ipCityIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('ipCityIsDisp_order', $vars['ipCityIsDisp'], $time + 3600 * 24 * 30);
            $vars['sessionIdIsDisp'] = $this->_request->get('sessionIdIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('sessionIdIsDisp_order', $vars['sessionIdIsDisp'], $time + 3600 * 24 * 30);
            $vars['udidIsDisp'] = $this->_request->get('udidIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('udidIsDisp_order', $vars['udidIsDisp'], $time + 3600 * 24 * 30);
            $vars['verIsDisp'] = $this->_request->get('verIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('verIsDisp_order', $vars['verIsDisp'], $time + 3600 * 24 * 30);
            $vars['userAgentIsDisp'] = $this->_request->get('userAgentIsDisp') != NULL ? 1 : 0;
            $cookieModel->setCookie('userAgentIsDisp_order', $vars['userAgentIsDisp'], $time + 3600 * 24 * 30);
        } else {
            $vars['addressIsDisp'] = $cookieModel->getCookie('addressIsDisp_order') ? 1 : 0;
            $vars['appVersionIsDisp'] = $cookieModel->getCookie('appVersionIsDisp_order') ? 1 : 0;
            $vars['ipProvinceIsDisp'] = $cookieModel->getCookie('ipProvinceIsDisp_order') ? 1 : 0;
            $vars['ipCityIsDisp'] = $cookieModel->getCookie('ipCityIsDisp_order') ? 1 : 0;
            $vars['ipIsDisp'] = $cookieModel->getCookie('ipIsDisp_order') ? 1 : 0;
            $vars['sessionIdIsDisp'] = $cookieModel->getCookie('sessionIdIsDisp_order') ? 1 : 0;
            $vars['udidIsDisp'] = $cookieModel->getCookie('udidIsDisp_order') ? 1 : 0;
            $vars['verIsDisp'] = $cookieModel->getCookie('verIsDisp_order') ? 1 : 0;
            $vars['userAgentIsDisp'] = $cookieModel->getCookie('userAgentIsDisp_order') ? 1 : 0;
        }

        $vars['orderId'] = trim($this->_request->get('orderId'));
        $vars['userName'] = trim($this->_request->get('userName'));
        $vars['fullName'] = trim($this->_request->get('fullName'));
        $vars['cellphone'] = trim($this->_request->get('cellphone'));
        $vars['province'] = trim($this->_request->get('province'));
        $vars['city'] = trim($this->_request->get('city'));
        $vars['county'] = trim($this->_request->get('county'));
        $vars['address'] = trim($this->_request->get('address'));
        $vars['udid'] = trim($this->_request->get('udid'));
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        $vars['sourceSubId'] = trim($this->_request->get('sourceSubId'));
        $vars['ip'] = trim($this->_request->get('ip'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $vars['isTest'] = trim($this->_request->get('isTest'));
        $vars['status'] = trim($this->_request->get('status'));
        $urls = $vars;

        $wheres[] = "`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."'";
        if ($vars['orderId'] != '') {
            $wheres[] = "`orderId` = '" . addslashes($vars['orderId']) . "'";
        }
        if ($vars['userName'] != '') {
            $wheres[] = "`userName` = '" . addslashes($vars['userName']) . "'";
        }
        if ($vars['fullName'] != '') {
            $wheres[] = "`fullName` = '" . addslashes($vars['fullName']) . "'";
        }
        if ($vars['cellphone'] != '') {
            $wheres[] = "`cellphone` = '" . addslashes($vars['cellphone']) . "'";
        }
        if ($vars['province'] != '') {
            $wheres[] = "`province` LIKE '%" . addslashes($vars['province']) . "%'";
        }
        if ($vars['city'] != '') {
            $wheres[] = "`city` LIKE '%" . addslashes($vars['city']) . "%'";
        }
        if ($vars['county'] != '') {
            $wheres[] = "`county` LIKE '%" . addslashes($vars['county']) . "%'";
        }
        if ($vars['address'] != '') {
            $wheres[] = "`address` LIKE '%" . addslashes($vars['address']) . "%'";
        }
        if ($vars['udid'] != '') {
            $wheres[] = "`udid` = '" . addslashes($vars['udid']) . "'";
        }
        if ($vars['osName'] != '') {
            $wheres[] = "`osName` = '" . addslashes($vars['osName']) . "'";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '" . addslashes($vars['sourceId']) . "'";
        }
        if ($vars['sourceSubId'] != '') {
            $wheres[] = "`sourceSubId` = '" . addslashes($vars['sourceSubId']) . "'";
        }
        if ($vars['ip'] != '') {
            $wheres[] = "`ip` = '" . addslashes($vars['ip']) . "'";
        }
        if ($vars['startTime'] != '') {
            $wheres[] = "`addTime` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`addTime` <= '{$vars['endTime']}' ";
        }
        if ($vars['isTest'] === '1' || $vars['isTest'] === '0') {
            $wheres[] = "`isTest` = '{$vars['isTest']}'";
        }
        if ($vars['status'] === '1' || $vars['status'] === '2') {
            $wheres[] = "`status` = '{$vars['status']}'";
        }

        $orderBy = "`addTime` DESC, `id` DESC";

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT * FROM `{$this->_model->getTableName()}` {$where} ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();
        
        foreach($rows as $key=>$row) {
            $row['itemXmlParseIsError'] = 0;
            $row['itemRows'] = array();
            try{
                $xml = @new SimpleXMLElement($row['itemData']);
                foreach($xml->item as $item) {
                    $itemRow = array();
                    foreach($item->attributes() as $key2=>$value) {
                        $itemRow[$key2] = (string) $value;
                    }
                    $row['itemRows'][] = $itemRow;
                }
            } catch(Exception $e) {
                $row['itemXmlParseIsError'] = 1;
            }
            $rows[$key] = $row;
        }

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }
}